package com.cop4331.social;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 4/12/2012 by Matt Szoke
 * 
 * Adapter to store profile for ChooseTutorActivity
 * 
 */
public class SimpleAdapter_Ext extends SimpleAdapter {

	// -----------------------------------------------
	// CONSTRUCTOR
	// -----------------------------------------------
	public SimpleAdapter_Ext(Context context,
			List<? extends Map<String, ?>> data, int resource, String[] from,
			int[] to) {
		super(context, data, resource, from, to);
	}

	// -----------------------------------------------
	// SETVIEWIMAGE
	// -----------------------------------------------
	public void setViewImage(ImageView v, String value) {

		super.setViewImage(v, value);

		// URL from ChooseTutorActivity
		URL url;

		try {

			url = new URL(value);

			// Establish Connection
			URLConnection conn = url.openConnection();
			conn.connect();

			// Retreive bitmap from URL
			InputStream is = conn.getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is);
			Bitmap bm = BitmapFactory.decodeStream(bis);

			// Shrink bitmap to half-size
			int width = (int) (bm.getWidth() * .5);
			int height = (int) (bm.getHeight() * .5);

			Bitmap bitmapOrig = Bitmap.createScaledBitmap(bm, width, height,
					false);

			// Set Bitmap
			v.setImageBitmap(bitmapOrig);

			// Close input streams
			bis.close();
			is.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}